<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态路由</title>
    <script src="https://cdn.bootcss.com/vue/2.4.2/vue.js"></script>
    <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
<body>
    <div id="app">
        <h1>Hello App</h1>
        <p>
            <router-link to="/foo/1">Go to Foo</router-link>
            <router-link to="/bar">Go to bar</router-link>
        </p>

        <router-view></router-view>
    </div>

<script>
    const Foo = {
        template : '<div>foo-{{$route.params.id}}</div>',
        watch : {
            '$route' (to, from) {
                console.log('watch---', to, from);
            }
        },
        beforeRouteUpdate  (to, from, next){
            console.log('before---', to, from, next)
        }
    }
    const Bar = {template : '<div>bar</div>'}

    const routes = [
        {path : '/foo/:id', component : Foo},
        {path : '/bar', component : Foo}
    ]

    const router = new VueRouter({
        routes  //缩写，相当于 routers : routers
    });

    const app = new Vue({
        router
    }).$mount("#app");
</script>
</body>
</html>